﻿using CommonEntity.CommonImageEntities;
using DbInfra;
using Grpc.Core;
using Newtonsoft.Json;
using ScienceRpcServe.Protos;
using ScienceRpcServe.Utils;
using ImageRecord = ScienceRpcServe.Protos.ImageRecord;

namespace ScienceRpcServe.Services;

public class HistoryImageRecordService : HistoryImageRecord.HistoryImageRecordBase
{
    private IImageRepository _imageRepository;

    private readonly ILogger<HistoryImageRecordService> _logger;


    public HistoryImageRecordService(IImageRepository imageRepository, ILogger<HistoryImageRecordService> logger)
    {
        _imageRepository = imageRepository;
        _logger = logger;
    }

    /// <summary>
    /// 图像记录删除
    /// </summary>
    /// <param name="request"></param>
    /// <param name="context"></param>
    /// <returns></returns>
    public override async Task<BoolResponse> DeleteImageRecord(IntRequest request, ServerCallContext context)
    {
        try
        {
            _logger.LogInformation($"{nameof(DeleteImageRecord)} | {JsonConvert.SerializeObject(request)}");
            _imageRepository.DeleteImageRecord(request.Value);
            return new BoolResponse() { IsSuccess = true };
        }
        catch (Exception e)
        {
            _logger.LogError($"{nameof(DeleteImageRecord)} | {JsonConvert.SerializeObject(e)}");
            return new BoolResponse() { Remark = e.Message };
        }
    }

    /// <summary>
    /// 获取图像总数
    /// </summary>
    /// <param name="request"></param>
    /// <param name="context"></param>
    /// <returns></returns>
    public override async Task<IntResponse> GetImageRecordCount(EmptyRequest request, ServerCallContext context)
    {
        try
        {
            _logger.LogInformation($"{nameof(GetImageRecordCount)} | {JsonConvert.SerializeObject(request)}");
            var count = _imageRepository.GetTotalRecordCount();

            if (count == 0)
            {
                for (var i = 0; i < 15; i++)
                {
                    _imageRepository.AddImageRecord(new CommonEntity.CommonImageEntities.ImageRecord()
                    {
                        PatientName = $"lily{i}",
                        SeriesDescription = "ad 8nm eb 57Vm i ",
                        CreateTime = DateTime.Now,
                        Dimension = "256*256*54",
                        Directory = @"D:\CC-Pros\data\S3"
                    });
                }
                count = _imageRepository.GetTotalRecordCount();
            }

            return new IntResponse() { Value = count };
        }
        catch (Exception e)
        {
            _logger.LogError($"{nameof(GetImageRecordCount)} | {JsonConvert.SerializeObject(e)}");
            return new IntResponse() { Remark = e.Message };
        }
    }


    /// <summary>
    /// 数据查找
    /// </summary>
    /// <param name="request"></param>
    /// <param name="context"></param>
    /// <returns></returns>
    public override async Task<RecordSearchResponse> FindImageRecords(RecordSearchRequest request, ServerCallContext context)
    {
        try
        {
            _logger.LogInformation($"{nameof(FindImageRecords)} | {JsonConvert.SerializeObject(request)}");
            var records = _imageRepository.QueryImageRecords(request.PageIndex, request.PageNum);
            var rs = new RecordSearchResponse();
            foreach (var rd in records)
            {
                rs.ImageRecords.Add(new ImageRecord()
                {
                    Id = rd.Id,
                    //PatientName = rd.PatientName,
                    SeriesDescription = rd.SeriesDescription,
                    CreateTime = rd.CreateTime.ToString(),
                    Dimension = rd.Dimension,
                    Directory = rd.Directory,
                });
            }

            return rs;
        }
        catch (Exception e)
        {
            _logger.LogError($"{nameof(FindImageRecords)} | {JsonConvert.SerializeObject(e)}");
            return new RecordSearchResponse(){Remark = e.Message };
        }
    }


}
